package com.oyjd.business.reply.activity;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.util.HashMap;
import java.util.Map;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
//import android.media.ThumbnailUtils;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.oyjd.R;
import com.oyjd.framework.Const;
import com.oyjd.framework.log.MyLog;
import com.oyjd.framework.util.HttpNet;

/**
 * 回复使用例子
 * 
 * @author 欧建东
 * @email oyjd@163.com
 * 
 */
public class ReplyDemo extends Activity {
	private static final String TAG = "ReplyActivity";
	private static final int CAMERA_CAPTURE = 1;
	private ImageView img;
	private EditText et;

	private File pic = new File(Const.APP_IMG_DIR + "photo.jpg");;

	// ThumbnailUtils.extractThumbnail(bitmap, 300, 200)

	// 压缩图片质量:
	// bitmap.compress(Bitmap.CompressFormat.JPEG, quality, fos);
	// 其中的quality为0~100, 可以压缩图片质量, 不过对于大图必须对图片resize
	//
	// 这个是等比例缩放:
	// bitmap = Bitmap.createScaledBitmap(bitmap, width, height, false);
	//
	// 这个是截取图片某部分:
	// bitmap = Bitmap.createBitmap(bitmap, x, y, width, height);

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.act_reply_demo);

		img = (ImageView) this.findViewById(R.id.img_capture);
		et = (EditText) this.findViewById(R.id.et_content);

		Button bt_capture = (Button) this.findViewById(R.id.bt_capture);
		bt_capture.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
				intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(pic));
				startActivityForResult(intent, CAMERA_CAPTURE);
			}
		});

		Button bt_reply = (Button) this.findViewById(R.id.bt_reply);
		bt_reply.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				try {
					Map<String, Object> para = new HashMap<String, Object>();
					para.put("myInfo", et.getText().toString());

					ByteArrayOutputStream oStream = new ByteArrayOutputStream();
					BitmapDrawable bd = (BitmapDrawable) img.getDrawable();
					if (bd != null) {
						Bitmap newbBitmap = Bitmap.createScaledBitmap(bd.getBitmap(), 300, 200, false);
						newbBitmap.compress(CompressFormat.JPEG, 100, oStream);
						byte[] b = oStream.toByteArray();
						oStream.close();
						//
						para.put("upload", new HttpNet.UploadFileVO("test.jpg", new ByteArrayInputStream(b)));
					}

				} catch (Exception e) {
					MyLog.e(TAG, e.getMessage(), e);
				}

			}
		});
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		// resultCode,-1表示拍照，0表示取消
		MyLog.i(TAG, "------[" + resultCode + "]");
		if (-1 == resultCode && requestCode == CAMERA_CAPTURE) {
			Bitmap bitmap = BitmapFactory.decodeFile(pic.getPath());
			Bitmap newbBitmap = Bitmap.createScaledBitmap(bitmap, 300, 200, false);
			img.setImageBitmap(newbBitmap);
			// img.setImageBitmap(ThumbnailUtils.extractThumbnail(bitmap, 300,
			// 200));

			// Bundle extras = data.getExtras();
			// if (extras != null) {
			// Bitmap b = (Bitmap) extras.get("data");
			// img.setImageBitmap(b);
			// }

		}

	}

}
